package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录功能
     *
     * @param employee 请求参数(用户名&密码)
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Employee employee, HttpSession session) {
        //1.获取用户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //2.判断用户名是否存在
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, username);
        Employee e = employeeService.getOne(wrapper);
        if (e == null) {
            return R.error("用户名不存在");
        }
        //3.判断密码是否正确
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!StringUtils.equals(password, e.getPassword())) {
            return R.error("密码错误");
        }
        //4.判断用户状态是否启用
        if (e.getStatus() == 0) {
            return R.error("用户已禁用");
        }
        //5.登录成功
        Long id = e.getId();
        session.setAttribute("employee",id);
        return R.success(e);
    }

    /**
     * 登出功能
     *
     * @return
     */
    @PostMapping("logout")
    public R logout(HttpSession session) {
        session.invalidate();
        return R.success("退出成功");
    }


    /**
     * 添加员工
     *
     * @return
     */
    @PostMapping
    public R save(@RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("page")
    public R selectPage(Integer page ,Integer pageSize,String name){
        Page<Employee> employeePage = new Page<>(page,pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByDesc("update_time");
        employeeService.page(employeePage,queryWrapper);
        return R.success(employeePage);
    }

    @PutMapping
    public R updateStatus(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("更改状态成功！");
    }

    @GetMapping("{id}")
    public R selectById(@PathVariable Long id){
        Employee e = employeeService.getById(id);
        return R.success(e);
    }
}
